import DBConnection from "../../service/unit/mysql";
import File from '../../service/file/File';

interface IController{
    anthology: string
    author: string
    title: string
}

/**
 * 负责写入文件,插入数据库,最后返回id
 * @param conn DBConnection实例
 * @param data 包含着必要信息
 * @return {promise}
 * */
export default async function create_article(conn:DBConnection, data:IController) {
    const CURRENT_UTC = getCurrentUTC();
    const TABLE_NAME = 'article';
    const {anthology, author, title} = data;
    const PATH = `./article/${CURRENT_UTC}${anthology}${title}${author}`;
    await File.writeFile(PATH, title, '');
    await conn.Insert.insert(TABLE_NAME, {anthology, author, title, path:PATH});
    const r =await conn.Query.queryWhere(TABLE_NAME,{anthology, author, title, path:PATH},['id','title']);
    console.log('r:',r);
    return r
}

function getCurrentUTC(){

    return Date.now()
}